/**
 *   _                                                          _   
 *  | | ____ _ _ __ _ __ ___   __ _  ___           _ __ ___ ___| |_ 
 *  | |/ / _` | '__| '_ ` _ \ / _` |/ __|  _____  | '__/ _ | __| __|
 *  |   < (_| | |  | | | | | | (_| | (__  |_____| | | |  __|__ \ |_ 
 *  |_|\_\__,_|_|  |_| |_| |_|\__,_|\___|         |_|  \___|___/\__|
 *         
 *  Copyright  2011 Kondra, Inc. 
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.kondra.karmac.rest
{
   import flash.utils.describeType;

   /**
    * Service notification handler object used to register service event
    * listeners to a service task.
    * 
    * @author Jared Kaplan
    */ 
   public class ServiceNotificationHandler
   {
      private var m_completeHandler:Function;
      private var m_errorHandler:Function;
      private var m_progressHandler:Function;
      
      public function ServiceNotificationHandler( completeHandler:Function = null, errorHandler:Function = null, progressHandler:Function = null )
      {
         m_completeHandler = completeHandler;
         m_errorHandler = errorHandler;
         m_progressHandler = progressHandler;
      }
      
      /**
       * Called by the service task when the service request is complete.
       * 
       * @param result           The service result object for this event
       */ 
      public function notifyComplete( result:ServiceResult ) : void {
         notifyHandler( m_completeHandler, result );
      }
      
      /**
       * Called by the service task when an error has occured while making
       * the service request.  
       * 
       * @param result           The service result object for this event
       */ 
      public function notifyError( result:ServiceResult ) : void {
         notifyHandler( m_errorHandler, result );
      }
      
      /**
       * Called by the service task while the service request is being made 
       * to report progress.
       * 
       * @param result           The service result object for this event
       */ 
      public function notifyProgress( result:ServiceResult ) : void {
         notifyHandler( m_progressHandler, result );
      }
      
      /**
       * Private method for dispatching the notification event to the handler.
       * 
       * @param handler          Handler function to dispatch the event to
       * @param result           The service result object for this event
       */ 
      private function notifyHandler( handler:Function, result:ServiceResult ) : void
      {
         if( handler != null )
         {
            if( handler.length == 0 )
               handler();
            
            else if( handler.length == 1 )
               handler( result );
            else
               throw new Error( "ServiceNotificationHandler:notifyHandler() : Invalid function prototype. Unexpected number of arguments." );
         }
      }
      
      //
      // accessor methods
      //
      public function hasNotificationComplete() : Boolean { return m_completeHandler != null; }
      public function hasNotificationError() : Boolean { return m_errorHandler != null; }
      public function hasNotificationProgress() : Boolean { return m_progressHandler != null; }
   }
}